package com.bit.cfdi.web.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.bit.cfdi.web.dao.DireccionDAO;
import com.bit.cfdi.web.domain.Direccion;

@Repository
public class DireccionDAOImpl extends HibernateDaoSupport implements
		DireccionDAO {

	@Autowired
	public void init(final SessionFactory sessionFactory) throws DataAccessException {
		setSessionFactory(sessionFactory);
	}
	
	@Transactional
	@Override
	public void save(final Direccion direccion) throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(direccion);
	}

	@Transactional
	@SuppressWarnings("unchecked")
	@Override
	public Direccion getById(final Long id) throws DataAccessException {
		List<Direccion> direcciones = getHibernateTemplate().find(
				" from Direccion as d" +
				" where d.idDireccion=?", id);
		return direcciones.size()>0?direcciones.get(0):null;
	}

	@Transactional
	@SuppressWarnings("unchecked")
	@Override
	public List<Direccion> getAll() throws DataAccessException {
		List<Direccion> direcciones = getHibernateTemplate().find(
				" from Direccion as d");
		return direcciones;
	}

}
